package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书!");
        System.out.println("请输入要删除图书的名字");
        String name=scanner.nextLine();
        int currenSize= bookList.getUsedSize();
        int index=0;
        int i=0;
        for( i=0;i<currenSize;i++){
            Book book=bookList.getPos(i);
            if(name.equals(book.getName())){
                index=i;
                break;
            }
        }
        if(i>=currenSize){
            System.out.println("没有你要删除的图书");
            return ;
        }
        for(int j=index;j<currenSize-1;j++){
            Book book=bookList.getPos(j+1);
            bookList.setBook(j,book);//把j+1下标的图书赋给了j
        }
        bookList.setBook(currenSize,null);//删除成功后，把最后一个空的元素制空。
        System.out.println("删除成功!");
    }
}
